package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.entity.ShoppingCart;
import com.itheima.mapper.ShoppingCartMapper;
import com.itheima.service.IShoppingCartService;
import com.itheima.util.Result;
import com.itheima.util.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * @author 老师好我是郁同学
 * @date 2022/10/10
 **/
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements IShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    /**
     * 添加商品
     * @param shoppingCart
     * @return
     */
    @Override
    public Result add(ShoppingCart shoppingCart) {
        //判断之前是否购买过
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        ShoppingCart selectOne = shoppingCartMapper.selectOne(lqw);
        if (selectOne == null) {
            //之前没有购买过，用户第一次添加
            int r = shoppingCartMapper.insert(shoppingCart);
            if (r<=0){
                return Result.error("添加失败");
            }
            return Result.success(shoppingCart);
        }
        //之前购买过，只需要增加数量即可
        selectOne.setNumber(selectOne.getNumber()+1);
        //将修改之后的购物车信息，保存到数据库中
        int r = shoppingCartMapper.updateById(selectOne);
        if (r<=0){
            return Result.error("添加失败");
        }
        return Result.success(selectOne);
    }

    @Override
    public Result clean() {
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        Long userId = UserHolder.getUserId();
        lqw.eq(ShoppingCart::getUserId,userId);
        int r = shoppingCartMapper.delete(lqw);
        if (r<=0){
            return Result.error("取消失败");
        }
        return Result.success("取消成功");
    }

    @Override
    public Result sub(ShoppingCart shoppingCart) {
        //根据套餐id查找购物车
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        ShoppingCart selectOne = shoppingCartMapper.selectOne(lqw);
        //判断购物车是否存在
        if (selectOne==null){
            //不存在返回错误信息
            return Result.error("购物车信息不能存在");
        }
        //购物车存在
        //判断套餐的数量
        if (!(selectOne.getNumber()>1)){
            //如果数量不大于1，删除购物信息的操作
            int r = shoppingCartMapper.delete(lqw);
            if (r<=0){
                return Result.error("取消失败");
            }
            return Result.success("取消成功");
        }
        //如果数量大于1，数量执行 -1的操作
        selectOne.setNumber(selectOne.getNumber()-1);
        int r = shoppingCartMapper.updateById(selectOne);
        if (r<=0){
            return Result.error("取消失败");
        }
        return Result.success("取消成功");
    }

    @Override
    public Result shoppingCartList() {
        //根据用户的id查找该用户的购物车
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        //
        lqw.eq(ShoppingCart::getUserId,UserHolder.getUserId());
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectList(lqw);
        return Result.success(shoppingCarts);
    }
}
